//! 内核错误类型定义

use thiserror::Error;

/// 内核错误类型
#[derive(Error, Debug)]
pub enum KernelError {
    #[error("插件错误: {0}")]
    Plugin(String),
    
    #[error("事件总线错误: {0}")]
    EventBus(String),
    
    #[error("资源管理错误: {0}")]
    Resource(String),
    
    #[error("配置错误: {0}")]
    Config(String),
    
    #[error("安全错误: {0}")]
    Security(String),
    
    #[error("查询错误: {0}")]
    Query(String),
    
    #[error("IO错误: {0}")]
    Io(#[from] std::io::Error),
    
    #[error("序列化错误: {0}")]
    Serialization(#[from] serde_json::Error),
    
    #[error("Arrow错误: {0}")]
    Arrow(#[from] arrow::error::ArrowError),
    
    #[error("内部错误: {0}")]
    Internal(String),
}

/// 内核结果类型
pub type Result<T> = std::result::Result<T, KernelError>;

impl KernelError {
    /// 创建插件错误
    pub fn plugin<S: Into<String>>(msg: S) -> Self {
        Self::Plugin(msg.into())
    }
    
    /// 创建事件总线错误
    pub fn event_bus<S: Into<String>>(msg: S) -> Self {
        Self::EventBus(msg.into())
    }
    
    /// 创建资源管理错误
    pub fn resource<S: Into<String>>(msg: S) -> Self {
        Self::Resource(msg.into())
    }
    
    /// 创建配置错误
    pub fn config<S: Into<String>>(msg: S) -> Self {
        Self::Config(msg.into())
    }
    
    /// 创建安全错误
    pub fn security<S: Into<String>>(msg: S) -> Self {
        Self::Security(msg.into())
    }
    
    /// 创建查询错误
    pub fn query<S: Into<String>>(msg: S) -> Self {
        Self::Query(msg.into())
    }
    
    /// 创建内部错误
    pub fn internal<S: Into<String>>(msg: S) -> Self {
        Self::Internal(msg.into())
    }
}

impl From<anyhow::Error> for KernelError {
    fn from(err: anyhow::Error) -> Self {
        KernelError::Internal(err.to_string())
    }
}
